<?php
/**
 * sample app
 * core
 * for downloading stuff
 * from australian crime db
 * and check what's happening
 */
class Cr_Download extends Blib_Class_Atomic
{
    /**
     * the url
     * @var str
     */
    protected $_url = '';


    /**
     * where to store the file
     * @var str
     */
    protected $_path = '';


    /**
     * the actual data
     * @var str
     */
    protected $_data = '';


    /**
     * constructor
     * @param str $url the url to grab
     * @param str $path path to dump the content to
     */
    public function __construct($url, $path)
    {
        $this->_url = $url;
        $this->_path = $path . md5($url);

    }// end __construct


    /**
     * download it...
     * @return void
     */
    public function download()
    {
        // first of all, is it already there?
        $expire = 30000; // 50 minutes caching time...
        $sep    = '<<<<!!SEP!!>>>>';
        $cont = @file_get_contents($this->_path);
        if (!empty($cont)) {
            $tmpAr = explode($sep, $cont);
            if (!empty($tmpAr[1])) {
                if ((int) $tmpAr[0] >= mktime()) {
                    $data = $tmpAr[1];
                    return unserialize($data);    // still good
                }
            }
        }

        // otherwise, redo the download...
        $exp = mktime() + $expire;
        $cont = @file_get_contents($this->_url);
        $this->_data = $cont;
        if (empty($cont)) {
            throw new Blib_Exception('Can not download from ' . $this->_url);
        }
        // otherwise, save and cache
        // parse:
        $cont = $this->parse();
        if (@file_put_contents($this->_path, $exp . $sep . serialize($cont))) {
            return $cont;
        }
        throw new Blib_Exception('Can not save file to ' . $this->_path);

    }// end download


    /**
     * parse the data
     * @return void
     */
    public function parse()
    {
        // 1st pass, the ones with images...
        $pattern = '/<tr><td>([a-zA-Z0-9 ]+)<\/td><td>([A-Z]+)<\/td><td>([^<]+)<\/td><td>([^<]+)<\/td><td>([^<]+)<\/td><td><a href="([^"]+)"/is';
        preg_match_all($pattern, $this->_data, $dataWithoutImages);
        $pattern = '/<tr><td>([a-zA-Z0-9 ]+)<\/td><td>([A-Z]+)<\/td><td>([^<]+)<\/td><td>([^<]+)' .
                   '<\/td><td><a href="([^"]+)">Image<\/a><\/td><td><a href="([^"]+)"/is';
        preg_match_all($pattern, $this->_data, $dataWithImages);
        // match up by using arrays
        unset($dataWithImages[0], $dataWithoutImages[0]);
        $db = array();
        // start it...
        $count = count($dataWithImages[1]);
        $id    = 0;
        for ($i = 0; $i < $count; $i ++) {
            $id ++;
            $db[$dataWithImages[2][$i]][] = new Cr_Data_Crime(array(
                'id'   => $id,
                'date' => $dataWithImages[1][$i],
                'title' => $dataWithImages[3][$i],
                'name' => $dataWithImages[4][$i],
                'image' => $dataWithImages[5][$i],
                'link' => $dataWithImages[6][$i],
            ));
        }
        // more
        $count = count($dataWithoutImages[1]);
        for ($i = 0; $i < $count; $i ++) {
            $id ++;
            $db[$dataWithoutImages[2][$i]][] = new Cr_Data_Crime(array(
                'id'   => $id,
                'date' => $dataWithoutImages[1][$i],
                'title' => $dataWithoutImages[3][$i],
                'name' => $dataWithoutImages[4][$i],
                'image' => $dataWithoutImages[5][$i],
                'link' => $dataWithoutImages[6][$i],
            ));
        }

        return $db;

    }// end parse

}// end Cr_Core_Download
?>